package com.market.cash.utils.helper

import com.market.cash.CashRootApps
import com.market.cash.utils.language.LocaleContract
import com.market.cash.utils.language.MultiLanguages
import org.json.JSONArray
import org.json.JSONObject

/**
 * CashRoot
 * @Author wwdeng
 * @CreateTime: 2024/10/17 12:04
 * @Annotation：自定义数据 帮助类
 */
object CashRootCustomConfigsHelper {

    /**
     * 自定义数据来源
     *     1：客服配置
     *
     * "customData": {
     *         "privacyPolicyLink": "https://www.baidu.com",
     *         "termsUseLink": "https://www.baidu.com",
     *         "isClearData": "YES",
     *         "licenseInfo": {
     *             "isShow": "YES",
     *             "logo": "https://divoire.s3.af-south-1.amazonaws.com/pic/0eec2975dd8d4494a73e091fd4dcfb90.jpg",
     *             "desc": "This App is operated by",
     *             "companyName": "company name"
     *         },
     *         "licenseInfo_sw": {
     *             "isShow": "YES",
     *             "logo": "https://divoire.s3.af-south-1.amazonaws.com/pic/0eec2975dd8d4494a73e091fd4dcfb90.jpg",
     *             "desc": "Programu hii inaendeshwa na",
     *             "companyName": "company name"
     *         },
     *         "noticeInfo": {
     *             "isShow": "YES",
     *             "noticeTitle": "Notice",
     *             "noticeText": "By applying, the Applicant allows the lender or its authorized representatives to verify the information provided by him/her via call and field verification. The information provided by the Applicant shall include name, age, gender, emergency contact information, residence information, place of business and/or employment.\n\nEach Applicant shall only be allowed to register a maximum of One(1) userID or account in the Lender’s Software platform. Any additional User ID or account application will be subject for rejection or deletion by the lender without prior notice."
     *         },
     *         "noticeInfo_sw": {
     *             "isShow": "YES",
     *             "noticeTitle": "Taarifa",
     *             "noticeText": "Lorem ipsum dolor sit amet, consectetur adipiscing elitsed do eiusmod tempor incididunt ut labore et doloremagna aliqua. Ut enim ad minim veniam, quis nostrudexercitation ullamco laboris nisi ut aliguip ex ea\n\ncommodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eufugiat nulla pariatur. Excepteur sint occaecat cupidatatnon proident, sunt in culpa qui offcia deserunt mollitanim id est laborum Lorem ipsum dolor sit amet, consectetur adipiscing elit."
     *         },
     *         "smsContentKeys":["loan","cash","credit"],
     *         "smsMobileKeys":["airtel","msafi","flexitz"],
     *         "workDate": "Monday to Sunday",
     *         "workTime": "9:30am to 19:30pm."
     *     },
     *
     */

    /**
     * 获取客服配置
     */
    fun getCashRootConfigData(): JSONObject? {
        val dataString = SharedHelper.getCashRootSPString(SharedHelper.CashRoot_SPKEY.CASHROOT_COMMON)
        if (dataString.isEmpty()){
            return null
        }
        val data = JSONObject(dataString)
        if (data.has("customize")){
            return data.optJSONObject("customize")
        }
        return null
    }

    /**
     * 获取客服配置里自定义数据
     * customData
     */
    fun getCashRootCustomData(): JSONObject?{
        val customize = getCashRootConfigData()
        if (customize != null) {
            return customize.optJSONObject("customData")
        }
        return null
    }

    /**
     * 获取启动权限
     */
    fun getCashRootPermissionsLaunch(): JSONArray?{
        val customize = getCashRootConfigData()
        if(customize != null) {
            val permissions = customize.optJSONObject("permissions")
            if (permissions != null) {
                return permissions.optJSONArray("permission")
            }
        }
        return null
    }

    /**
     * 获取首页权限
     */
    fun getCashRootPermissionsHome(): JSONArray?{
        val customize = getCashRootConfigData()
        if(customize != null) {
            val permissions = customize.optJSONObject("permissions")
            if (permissions != null) {
                return permissions.optJSONArray("home")
            }
        }
        return null
    }

    /**
     * 获取所有权限列表 = 启动权限 + 首页权限
     */
    fun getCashRootPermissionsList(): ArrayList<String>{
        val permissionList = arrayListOf<String>()
        //启动权限
        val permissionLaunch = getCashRootPermissionsLaunch()
        if (permissionLaunch != null){
            for (i in 0..<permissionLaunch.length()){
                permissionList.add(permissionLaunch.optString(i))
            }
        }

        //首页权限
        val permissionHome = getCashRootPermissionsHome()
        if (permissionHome != null){
            for (i in 0..<permissionHome.length()){
                val str = permissionHome.optString(i)
                if (permissionList.contains(str)){
                    continue
                }
                permissionList.add(str)
            }
        }
        return permissionList
    }

    /**
     * 获取隐私协议链接
     */
    fun getCashRootPrivacyPolicyLink(): String{
        val customData = getCashRootCustomData() ?: return ""
        return customData.optString("privacyPolicyLink","")
    }

    /**
     * 获取用户使用协议
     */
    fun getCashRootTermsUseLink(): String{
        val customData = getCashRootCustomData() ?: return ""
        if (customData.has("termsUseLink")){
            return customData.optString("termsUseLink","")
        }
        return ""
    }

    /**
     * 是否显示清空数据按钮
     */
    fun isCashRootClearUserData(): Boolean{
        val customData = getCashRootCustomData() ?: return false
        if (customData.has("isClearData")){
            return customData.optString("isClearData","NO") == "YES"
        }
        return false
    }

    /**
     * 获取牌照信息
     */
    fun getCashRootLicenseInfo(): JSONObject?{
        val customData = getCashRootCustomData() ?: return null
        var licenseData: JSONObject? = null
        if (customData.has("licenseInfo")){
            licenseData = customData.optJSONObject("licenseInfo")
        }

        //是否是 斯瓦希里语
        val curtLocale = MultiLanguages.getAppLanguage(CashRootApps.app.getAppContext())
        if (customData.has("licenseInfo_sw") && curtLocale.language == LocaleContract.getSwahiliLocale().language){
            licenseData = customData.optJSONObject("licenseInfo_sw")
        }
        return licenseData
    }

    /**
     * 是否显示牌照信息
     */
    fun isCashRootShowLicense(): Boolean{
        val licenseInfo = getCashRootLicenseInfo() ?: return false
        return licenseInfo.optString("isShow") == "YES"
    }

    /**
     * 审核中、放款中是否显示公告信息
     */
    fun isCashRootAuditNotice(): Boolean{
        val customData = getCashRootCustomData() ?: return false
        if (customData.has("noticeInfo")){
            val noticeInfo = customData.optJSONObject("noticeInfo") ?: return false
            return noticeInfo.optString("isShow","NO") == "YES"
        }
        return false
    }

    /**
     * 获取 审核中、放款中 显示的公告信息
     */
    fun getCashRootAuditNoticeInfo(): JSONObject?{
        val customData = getCashRootCustomData() ?: return null

        var noticeData: JSONObject? = null
        if (customData.has("noticeInfo")){
            //默认为英语
            noticeData = customData.optJSONObject("noticeInfo")
        }

        //是否是 斯瓦希里语
        val curtLocale = MultiLanguages.getAppLanguage(CashRootApps.app.getAppContext())
        if (customData.has("noticeInfo_sw") && curtLocale.language == LocaleContract.getSwahiliLocale().language){
            noticeData = customData.optJSONObject("noticeInfo_sw")
        }
        return noticeData
    }

    /**
     * SMS Content 关键字过滤
     */
    fun getCashRootSmsContentKeys(): MutableSet<String>?{
        val customData = getCashRootCustomData() ?: return null
        if (customData.has("smsContentKeys")){
            val smsContentKeys = customData.optJSONArray("smsContentKeys")
            if (smsContentKeys != null) {
                val keysSet = mutableSetOf<String>()
                for (i in 0..<smsContentKeys.length()){
                    keysSet.add(smsContentKeys.optString(i))
                }
                return keysSet
            }
        }
        return null
    }


    /**
     * SMS Mobile 关键字过滤
     */
    fun getCashRootSmsMobileKeys(): MutableSet<String>?{
        val customData = getCashRootCustomData() ?: return null
        if (customData.has("smsMobileKeys")){
            val smsContentKeys = customData.optJSONArray("smsMobileKeys")
            if (smsContentKeys != null) {
                val keysSet = mutableSetOf<String>()
                for (i in 0..<smsContentKeys.length()){
                    keysSet.add(smsContentKeys.optString(i))
                }
                return keysSet
            }
        }
        return null
    }
}